package cn.yuanqiao.web.controller.archive.tongji;

import cn.yuanqiao.archive.tongji.service.TongJiService;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * @author zjj
 * @date 2023/7/28
 */
@RestController
@RequestMapping("/archive/tongji")
public class TongJiController extends BaseController {

    @Autowired
    private TongJiService tongJiService;

    /**
     * 门类统计
     * @param startTime
     * @param endTime
     * @return
     */
    @Log(title = "门类统计", businessType = BusinessType.SELECT)
    @PostMapping("/menlei")
    public AjaxResult menlei(@Param("startTime")String startTime,@Param("endTime")String endTime){
        JSONArray array = tongJiService.menlei(startTime,endTime);
        return AjaxResult.success(array);
    }

    /**
     * 库存统计
     * @param startTime
     * @param endTime
     * @return
     */
    @Log(title = "库存统计", businessType = BusinessType.SELECT)
    @PostMapping("/kucun")
    public AjaxResult kucun(@Param("startTime")String startTime,@Param("endTime")String endTime,@Param("deptId")String deptId){
        JSONObject array = tongJiService.kucun(startTime,endTime, StringUtils.isNotEmpty(deptId) ? Long.valueOf(deptId):null);
        return AjaxResult.success(array);
    }

    /**
     * 首页库存统计
     * @return
     */
    @Log(title = "首页库存统计", businessType = BusinessType.SELECT)
    @PostMapping("/symenlei")
    public AjaxResult ysmenlei(){
        JSONArray array = tongJiService.ysmenlei();
        return AjaxResult.success(array);
    }

    /**
     * 首页每日访问量
     * @return
     */
    @Log(title = "首页每日访问量", businessType = BusinessType.SELECT)
    @PostMapping("/syfwl")
    public AjaxResult ysfwl(){
        JSONArray array = tongJiService.ysfwl();
        return AjaxResult.success(array);
    }

    /**
     * 首页我的借阅
     * @return
     */
    @Log(title = "首页我的借阅", businessType = BusinessType.SELECT)
    @PostMapping("/sywdjy")
    public AjaxResult wdjy(){
        JSONArray array = tongJiService.wdjy();
        return AjaxResult.success(array);
    }

    /**
     * 各类档案归档情况对比（年）
     * @return
     */
    @Log(title = "首页各类档案归档情况对比（年）", businessType = BusinessType.SELECT)
    @PostMapping("/sygdqk")
    public AjaxResult sygdqk(){
        JSONObject array = tongJiService.sygdqk();
        return AjaxResult.success(array);
    }

    /**
     * 每日借阅量
     * @return
     */
    @Log(title = "首页每日借阅量", businessType = BusinessType.SELECT)
    @PostMapping("/symrjyl")
    public AjaxResult symrjyl(){
        JSONArray array = tongJiService.symrjyl();
        return AjaxResult.success(array);
    }

    /**
     * 首页信息
     * @return
     */
    @Log(title = "首页信息", businessType = BusinessType.SELECT)
    @PostMapping("/syInfo")
    public AjaxResult syInfo(){
        JSONObject array = tongJiService.syInfo();
        return AjaxResult.success(array);
    }

    /**
     * 利用统计
     * @return
     */
    @Log(title = "利用统计", businessType = BusinessType.SELECT)
    @PostMapping("/lytj")
    public AjaxResult lytj(@Param("startTime")String startTime,@Param("endTime")String endTime){
        JSONArray array = tongJiService.lytj(startTime,endTime);
        logger.info(array.toJSONString());
        return AjaxResult.success(array);
    }

    /**
     * 数据来源统计
     */
    @Log(title = "数据来源统计", businessType = BusinessType.SELECT)
    @PostMapping("/sjlytj")
    public AjaxResult lytj(@RequestParam("startTime")String startTime, @RequestParam("endTime")String endTime,@RequestParam("ids") String ids){
        if(StringUtils.isNotEmpty(endTime)){
            endTime = endTime + " 59:59:59";
        }
        JSONArray array = tongJiService.sjlytj(startTime,endTime,ids);
        logger.info(array.toJSONString());
        return AjaxResult.success(array);
    }

    @Log(title = "访问量统计", businessType = BusinessType.SELECT)
    @PostMapping("/fwltj")
    public AjaxResult fwltj(@RequestParam("startTime")String startTime, @RequestParam("endTime")String endTime){
        List<Map> array = tongJiService.fwltj(startTime,endTime);
        return AjaxResult.success(array);
    }

    @Log(title = "存储空间统计", businessType = BusinessType.SELECT)
    @PostMapping("/storageSpaceTj")
    public AjaxResult storageSpaceTj(){
        Map<String,Integer> array = tongJiService.storageSpaceTj();
        return AjaxResult.success(array);
    }

    @Log(title = "全宗统计", businessType = BusinessType.SELECT)
    @PostMapping("/qztj")
    public AjaxResult qztj(@RequestParam(value = "startTime",required = false)String startTime,
                           @RequestParam(value ="endTime",required = false)String endTime) {
        com.alibaba.fastjson.JSONArray array = tongJiService.qztj(startTime,endTime);
        return AjaxResult.success(array);
    }


    @Log(title = "档案全宗统计", businessType = BusinessType.SELECT)
    @PostMapping("/dagqztj")
    public AjaxResult dagqztj(@RequestParam(value = "startTime",required = false)String startTime,
                              @RequestParam(value ="endTime",required = false)String endTime) {
        com.alibaba.fastjson.JSONArray array = tongJiService.dagqztj(startTime,endTime);
        return AjaxResult.success(array);
    }

    @Log(title = "利用满意度统计", businessType = BusinessType.SELECT)
    @PostMapping("/lymydtj")
    public AjaxResult lymydtj(@RequestParam(value = "startTime",required = false)String startTime,
                              @RequestParam(value ="endTime",required = false)String endTime) {
        com.alibaba.fastjson.JSONArray array = tongJiService.lymydtj(startTime,endTime);
        return AjaxResult.success(array);
    }

    @Log(title = "格式统计", businessType = BusinessType.SELECT)
    @PostMapping("/gstj")
    public AjaxResult gstj(@RequestParam(value = "startTime",required = false)String startTime,
                           @RequestParam(value ="endTime",required = false)String endTime) {
        Map<String, Object> array = tongJiService.gstj(startTime,endTime);
        return AjaxResult.success(array);
    }

    @Log(title = "年度统计", businessType = BusinessType.SELECT)
    @PostMapping("/ndtj")
    public AjaxResult ndtj(@RequestParam(value = "startTime",required = false)String startTime,
                           @RequestParam(value ="endTime",required = false)String endTime) {
        Map<String, Object> array = tongJiService.ndtj(startTime,endTime);
        return AjaxResult.success(array);
    }

}
